<?php
class classPaging{
    private $ForceCategory='';

    function  __construct($SetForceCategory='') {
        $this->ForceCategory = $SetForceCategory;
    }

    public function showPageNumbers($sSQL, $iActualPage, $sOrdering){
        $SQL_Result = mysql_query($sSQL);
        $SQL_Result_Count = mysql_fetch_row($SQL_Result);
        $SQL_Result_Count[0];        
        $iPagesCount =$SQL_Result_Count[0]/_ITEMS_COUNT_IN_SINGLE_PAGE_;  


        //koniec funkcie, ak nie je potrebne strankovanie
        if($SQL_Result_Count[0]<=_ITEMS_COUNT_IN_SINGLE_PAGE_)
          return '';
        
        //----------------------------------------------------------------------
        //vytvorenie hranic vypisu
        $dolnaHranica = $iActualPage - ($iActualPage%_MAXIMUM_PAGES_COUNT_);
        $hornaHranica = $dolnaHranica + _MAXIMUM_PAGES_COUNT_;
        
        $result = '<center>';
        
        //generovanie vypisu        
        if($dolnaHranica<1)
          $result .= '';
        else
          $result .= $this->getSinglePageLink($dolnaHranica-1, _i18n_paging_PRIOR_, $sOrdering);
        
        //----------------------------------------------------------------------
        //vypis stranok - ak sa najde cislo aktualnej stranky stranky - toto bude zvyraznene
        
        if($hornaHranica>$iPagesCount) $max = $iPagesCount; else $max = $hornaHranica;
        for($iPageIndex=$dolnaHranica; $iPageIndex<($max); $iPageIndex++)
          if($iPageIndex==$iActualPage)
            $result .= '  <font style="font-size:120%"><b>'.$this->getSinglePageLink($iPageIndex, $iPageIndex+1, $sOrdering).'</b></font>';
          else
            $result .= '  '.$this->getSinglePageLink($iPageIndex, $iPageIndex+1, $sOrdering);
        
        
        //----------------------------------------------------------------------
        //najblizsia horna hranica kvanta
        
        if($hornaHranica<$iPagesCount)
          $result .= $this->getSinglePageLink($iPageIndex, _i18n_paging_NEXT_, $sOrdering);
        else
          $result .= '';
          
        $result .= '</center>';
        
        return $result;
    }
    
    private function getSinglePageLink($iPageIndex, $sText, $sOrdering){
        $_URL 	 = new classURL();
        $_Follow = new classFollow();

        if($this->ForceCategory=='')
            $sCategoryValue = $_URL->GetValueFromURL($_URL->GetProduct(), 'category');
        else
            $sCategoryValue = $this->ForceCategory;
        
        $result  =' 
            <a href="'.
                $_URL->SetURL('',$sOrdering._URL_VAL_DELIMITER_.$sCategoryValue._URL_VAL_DELIMITER_.'0'._URL_VAL_DELIMITER_.$iPageIndex,'').'
               "
              '.$_Follow->getJS(_i18n_paging_YUO_CAN_SHOW_PAGE_CONTENT_BY_CLICK_).'
            >
              '.$sText.'
            </a>
        ';	

        return $result;
    }

    //--------------------------------------------------------------------------
    //                                AJAX
    //--------------------------------------------------------------------------
    public function showPageNumbersByAJAX($sSQL, $iActualPage){

        if(trim($sSQL)=='') return '';

        $SQL_Result = mysql_query($sSQL);
        $SQL_Result_Count = mysql_fetch_row($SQL_Result);
        $SQL_Result_Count[0];
        $iPagesCount =$SQL_Result_Count[0]/_ITEMS_COUNT_IN_SINGLE_PAGE_;


        //koniec funkcie, ak nie je potrebne strankovanie
        if($SQL_Result_Count[0]<=_ITEMS_COUNT_IN_SINGLE_PAGE_)
          return '';

        //----------------------------------------------------------------------
        //vytvorenie hranic vypisu
        $dolnaHranica = $iActualPage - ($iActualPage%_MAXIMUM_PAGES_COUNT_);
        $hornaHranica = $dolnaHranica + _MAXIMUM_PAGES_COUNT_;

        $result = '<center>';

        //generovanie vypisu
        if($dolnaHranica<1)
          $result .= '';
        else
          $result .= $this->getSinglePageLinkForAJAX($dolnaHranica-1, _i18n_paging_PRIOR_);

        //----------------------------------------------------------------------
        //vypis stranok - ak sa najde cislo aktualnej stranky stranky - toto bude zvyraznene

        if($hornaHranica>$iPagesCount) $max = $iPagesCount; else $max = $hornaHranica;
        for($iPageIndex=$dolnaHranica; $iPageIndex<($max); $iPageIndex++)
          if($iPageIndex==$iActualPage)
            $result .= '  <font style="font-size:120%"><b>'.$this->getSinglePageLinkForAJAX($iPageIndex, $iPageIndex+1).'</b></font>';
          else
            $result .= '  '.$this->getSinglePageLinkForAJAX($iPageIndex, $iPageIndex+1);


        //----------------------------------------------------------------------
        //najblizsia horna hranica kvanta
        if($hornaHranica<$iPagesCount)
          $result .= $this->getSinglePageLinkForAJAX($iPageIndex, _i18n_paging_NEXT_);
        else
          $result .= '';

        $result .= '</center>';

        return $result;
    }

    private function getSinglePageLinkForAJAX($iPageIndex, $sText){
        $result  ='
            <a href="'._URL_INACTIVE_ADDR_.'" onclick="
                document.getElementById(\'ActualPage\').value='.$iPageIndex.';
                classProductSearch();
            ">
                '.$sText.'
            </a>';

        return $result;
    }
}
?>
